package com.dao;

import com.entity.Goods;
import com.entity.Report;
import com.entity.Student;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import java.util.List;

/**
 * 举报实体的 MyBatis 映射接口。
 */
@Mapper
public interface ReportDao {

    /**
     * 根据学生和商品查询举报记录。
     */
    @Select("SELECT r.id, r.created_at AS createTime, r.updated_at AS updateTime, " +
            "r.reason AS content, r.status, r.admin_notes AS adminNotes, " +
            "s.id AS student_id, s.username AS student_name, " +
            "p.id AS goods_id, p.name AS goods_name " +
            "FROM reports r " +
            "LEFT JOIN users s ON r.reporter_user_id = s.id " +
            "LEFT JOIN products p ON r.product_id = p.id " +
            "WHERE r.reporter_user_id = #{student.id} AND r.product_id = #{goods.id}")
    @Results(id = "ReportResultMap", value = {
        @Result(property = "id", column = "id", id = true),
        @Result(property = "createTime", column = "createTime"),
        @Result(property = "updateTime", column = "updateTime"),
        @Result(property = "reason", column = "content"),
        @Result(property = "status", column = "status"),
        @Result(property = "adminNotes", column = "adminNotes"),
        @Result(property = "student.id", column = "student_id"),
        @Result(property = "student.username", column = "student_name"),
        @Result(property = "goods.id", column = "goods_id"),
        @Result(property = "goods.name", column = "goods_name")
    })
    Report findByStudentAndGoods(@Param("student") Student student, @Param("goods") Goods goods);

    /**
     * 保存举报记录。
     * 需要在 ReportMapper.xml 中配置对应 id="save" 的 <insert> 语句。
     *
     * @param report 要保存的举报对象。
     * @return 受影响的行数。
     */
    @Insert("INSERT INTO reports (reporter_user_id, product_id, reason, status, admin_notes, created_at, updated_at) " +
            "VALUES (#{student.id}, #{goods.id}, #{reason}, #{status}, #{adminNotes}, NOW(), NOW())")
    @Options(useGeneratedKeys=true, keyProperty="id")
    int save(Report report);

    /**
     * 查询所有举报（管理员使用）
     */
    @Select("SELECT r.id, r.created_at AS createTime, r.updated_at AS updateTime, " +
            "r.reason AS content, r.status, r.admin_notes AS adminNotes, " +
            "s.id AS student_id, s.username AS student_name, " +
            "p.id AS goods_id, p.name AS goods_name " +
            "FROM reports r " +
            "LEFT JOIN users s ON r.reporter_user_id = s.id " +
            "LEFT JOIN products p ON r.product_id = p.id " +
            "ORDER BY r.created_at DESC")
    @ResultMap("ReportResultMap")
    List<Report> findAll();

    /**
     * 根据 ID 删除举报（管理员使用）
     */
    @Delete("DELETE FROM reports WHERE id = #{id}")
    int deleteById(Long id);


} 